﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HML.Demo
{
    public partial class ImageCropperForm : ItemFormTemplate
    {
        public ImageCropperForm()
        {
            InitializeComponent();
            this.Load += new System.EventHandler(this.Form_Load);
            this.Shown += new System.EventHandler(this.Form_Shown);
        }

        #region 实现

        public override ItemFormTemplateViewObject AsynchRequestTask(ItemFormTemplateViewObject view)
        {
            System.Threading.Thread.Sleep(300);
            view.ViewData = DemoHelper.GetDescription(AppDomain.CurrentDomain.BaseDirectory + "例子\\控件\\ImageCropperForm\\ImageCropperForm.rtf");
            return view;
        }

        public override void DataBind(ItemFormTemplateViewObject viewObject)
        {
            this.introduceBrowser.Rtf = viewObject.ViewData.ToString();
        }

        public override void ShowView()
        {
            base.ShowView();
        }

        public override void HideView()
        {
            base.HideView();
        }

        public override void CloseView()
        {
            base.CloseView();
        }

        #endregion

        #region 属性面板

        private void Form_Load(object sender, EventArgs e)
        {
            this.controlContainer.SizeChanged += this.ControlContainer_SizeChanged;

            this.demo_control.ImageScaleRatio = 100;
            this.demo_control.CropperAspectRatio = null;
            this.demo_control.CropperRectangleChanged += this.Demo_control_CropperRectangleChanged;

            this.image_buttonPlus.Click += this.Image_buttonPlus_Click;

            this.imageScaleRatio_label.Text = this.demo_control.ImageScaleRatio.ToString();
            this.imageScaleRatio_trackBarPlus.Value = (int)this.demo_control.ImageScaleRatio;
            this.imageScaleRatio_trackBarPlus.ValueChanged += this.ImageScaleRatio_trackBarPlus_ValueChanged;

            DropDownListPlusItemCollection list = new DropDownListPlusItemCollection();
            list.Add(new DropDownListPlusItem() { Text = "自由尺寸", Tag = null });
            list.Add(new DropDownListPlusItem() { Text = "4:3", Tag = 1.3333333333333 });
            list.Add(new DropDownListPlusItem() { Text = "16:9", Tag = 1.7777777777777 });
            this.cropperRatio_dropDownListPlus.BindItemsSource(list, list[0]);
            this.cropperRatio_dropDownListPlus.SelectedItemsChanged += this.CropperAspectRatio_dropDownListPlus_SelectedItemsChanged;

            this.sourceCropper_buttonPlus.Click += this.SourceCropper_buttonPlus_Click;

            this.zoomCropper_buttonPlus.Click += this.ZoomCropper_buttonPlus_Click;

        }

        private void Form_Shown(object sender, EventArgs e)
        {
            this.splitContainer.SplitterDistance = Math.Max(this.splitContainer.Width / 4, this.splitContainer.Width - MainForm.GlobalSplitWidth);
            this.splitContainer.SplitterMoved += this.SplitContainer_SplitterMoved;

            MainForm.LoadAssistTabControl(this.assistTabControl);
        }

        private void SplitContainer_SplitterMoved(object sender, SplitterEventArgs e)
        {
            SplitContainer control = (SplitContainer)sender;
            MainForm.GlobalSplitWidth = this.splitContainer.Width - control.SplitterDistance;
        }

        private void Demo_control_CropperRectangleChanged(object sender, EventArgs e)
        {
            this.cropperRectangle_label.Text = this.demo_control.GetCropperScaleRectangle().ToString();
        }

        private void CropperAspectRatio_dropDownListPlus_SelectedItemsChanged(object sender, EventArgs e)
        {
            DropDownListPlus control = (DropDownListPlus)sender;
            if (control.SelectedItem.Tag == null)
            {
                this.demo_control.CropperAspectRatio = null;
            }
            else
            {
                this.demo_control.CropperAspectRatio = float.Parse(control.SelectedItem.Tag.ToString());
            }
        }

        private void ControlContainer_SizeChanged(object sender, EventArgs e)
        {
            Panel control = (Panel)sender;
            this.demo_control.Location = new Point((control.Width - this.demo_control.Width) / 2, (control.Height - this.demo_control.Height) / 2);
        }

        private void Image_buttonPlus_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            {
                openFileDialog1.Filter = "(*.jpg*.png*.bmp)|*.jpg;*.png;*.bmp";
                openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.CommonPictures);
                openFileDialog1.RestoreDirectory = true;
                openFileDialog1.FileName = "";//对话框弹出时清空文件名

                if (openFileDialog1.ShowDialog() == DialogResult.OK)//也可以使用FileOk事件
                {
                    this.demo_control.Image = (Bitmap)Image.FromFile(openFileDialog1.FileName);

                }
            }
            openFileDialog1.Dispose();
        }

        private void ImageScaleRatio_trackBarPlus_ValueChanged(object sender, EventArgs e)
        {
            TrackBarPlus control = (TrackBarPlus)sender;
            this.demo_control.ImageScaleRatio = control.Value;
            this.imageScaleRatio_label.Text = this.demo_control.ImageScaleRatio.ToString();
        }

        private void SourceCropper_buttonPlus_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            saveFileDialog1.RestoreDirectory = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Bitmap bmp = demo_control.CropperSourceImage(true);

                ((Image)(bmp)).Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);

                bmp.Dispose();
            }
            saveFileDialog1.Dispose();
        }

        private void ZoomCropper_buttonPlus_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
            saveFileDialog1.RestoreDirectory = true;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                Bitmap bmp = demo_control.CropperSourceImage(false);

                ((Image)(bmp)).Save(saveFileDialog1.FileName, System.Drawing.Imaging.ImageFormat.Png);

                bmp.Dispose();
            }
            saveFileDialog1.Dispose();
        }

        #endregion

    }
}
